Skip to content

fix(input): wait for an ST split across reads - #201

Open
jtackaberry wants to merge 1 commit into
charmbracelet:mainfrom
jtackaberry:fix/osc5522
Open

jtackaberry wants to merge 1 commit into
charmbracelet:mainfrom
jtackaberry:fix/osc5522

Conversation

@jtackaberry

Copy link
Copy Markdown

Fixes #200.

If a read ends between the ESC and the \ of an OSC or APC terminator, parseOsc and parseStTerminated treat the ESC as cancelling the sequence. The sequence gets dropped, and the \ comes through as a key.

An ESC at the very end of the buffer doesn't tell you anything yet, since the next byte decides it. So both parsers now return a new unexported incompleteEvent there, and scanEvents waits for more input, like it does for any other partial sequence:

  • If \ arrives, the sequence completes as usual.
  • If something else arrives, the sequence is cancelled, same as before.
  • If the escape timeout passes with nothing else, the sequence is dropped as cancelled. That keeps the old behavior for a bare trailing ESC, which is why the existing ignored osc case in TestReadInput still passes unchanged.

Inside a bracketed paste, the same wait applies.

Testing

TestReadInputSplitTerminator splits an OSC 5522 packet and an APC sequence just before the final backslash, with an a after each:

go test -run TestReadInputSplitTerminator .
  • Before: fails. Each sequence comes out as a \ key followed by a.
  • After: passes. Each sequence comes out whole, followed by a. The full suite passes too, including with -race.

With the programs from the issue:

  • The pipe example: prints uv.UnknownOscEvent instead of a \ key.
  • kittyread: it no longer finds a stray backslash, however many rounds it runs.

Not included

DCS has a related problem. A DCS reply split at the same point comes out without its closing backslash, followed by a separate \ key, both before and after this change. parseDcs handles the terminator differently, so I've left it for a separate change. Happy to add it here if you'd rather.

  • I have read CONTRIBUTING.md.
  • I have created a discussion that was approved by a maintainer (for new features).

AI Disclosure

AI (Opus 5.5) was used to assist the creation of this PR.

A read that ended between the ESC and the backslash of an OSC or APC
terminator made the decoder cancel the sequence, dropping it and
reporting the backslash as a key. Long replies cross read boundaries
constantly (kitty's OSC 5522 clipboard data arrives in thousands of
5 KiB packets), so the sequence is incomplete, not cancelled. The
reader now waits for the rest, and only treats it as cancelled if the
escape timeout passes first.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Large images pasted via OSC 5522 are sometimes silently corrupted

1 participant